package com.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.dto.SetmealDto;
import com.reggie.entity.Setmeal;
import com.reggie.entity.SetmealDish;
import com.reggie.mapper.SetmealMapper;
import com.reggie.service.SetmealDishService;
import com.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @program: reggie
 * @author:
 * @create: 2022-09-29 21:11
 **/
@Service
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 添加套餐
     * 对于setmeal和setmealDish表中的数据的保存
     *
     * @param setmealDto
     */
    @Override
    public void addSetmeal(SetmealDto setmealDto) {
        this.save(setmealDto);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getName,setmealDto.getName());
        //获取添加菜品的id名称
        Setmeal one = this.getOne(wrapper);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            //setmealDish表中setmealId的赋值
            setmealDish.setSetmealId(one.getId());
        }
        /*setmealDishes.stream().map((res)->{
            res.setSetmealId(one.getId());
            return res;
        }).collect(Co);*/
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 修改套餐的数据
     * 对于setmeal和setmealDish表中的数据的保存
     * 对于setmealDish表中的数据进行删除（非逻辑删除），再将数据进行插入
     * @param setmealDto
     */
    @Override
    public void updateSetmeal(SetmealDto setmealDto) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getId,setmealDto.getId());

        //修改setmeal表中的数据
        this.update(setmealDto,wrapper);

        //将setmealDto中的数据进行删除
        LambdaQueryWrapper<SetmealDish> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(setmealWrapper);

        //将修改的数据添加setmealDish表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
            /*想要通过两个条件来实现该功能，---不可行---
            LambdaQueryWrapper<SetmealDish> dishWrapper = new LambdaQueryWrapper<>();
            //需要两个条件来确定该数据，一个条件在下一次循环会将上一条数据进行覆盖，因为在setmealDish表中对应的id是相同的
            dishWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
            dishWrapper.eq(SetmealDish::getDishId,setmealDish.getDishId());
            setmealDishService.saveOrUpdate(setmealDish,dishWrapper);*/
        }
        setmealDishService.saveBatch(setmealDishes);

    }

    /**
     * 套餐数据的删除
     *
     * @param ids
     */
    @Override
    public void deleteSetmeal(Long[] ids) {
        List<Long> list = Arrays.asList(ids);
        this.removeByIds(list);
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        for (Long id : ids) {
            wrapper.eq(SetmealDish::getSetmealId,id);
            setmealDishService.remove(wrapper);
        }

    }
}
